<template>
	<u-popup v-if="list.length" :show="show" mode="bottom" round="20rpx" :closeable="true" @close="handleShow(false)">
		<view class="container">
			<view class="title">
				请选择优惠劵
			</view>
			<scroll-view class="content" scroll-y="true">
				<view class="item" v-for="(item, index) in list" :key="index" @click="change(item, index)">
					<view class="item-left">
						<view class="price">
							￥<span>{{Number(item.arrive_price).toFixed(0)}}</span>
						</view>
						<view class="term">
							满{{Number(item.threshold_price).toFixed(0)}}可用
						</view>
					</view>
					<view class="item-middle">
						<view class="name">
							{{item.title}}
						</view>
						<view class="desc">
							{{item.content}}
						</view>
					</view>
					<view class="item-right">
						<radio :value="index" style="transform:scale(0.7)" :checked="index === selectIndex" color="#FF7300" />
					</view>
				</view>
			</scroll-view>
			<view class="btn" @click="handleConfirm">
				确定
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		name: 'CouponPopup',
		props: {
			value: {
				type: Boolean,
				default: false
			},
			list: {
				type: Array,
				default: []
			},
			// 默认选择的优惠券ID
			defaultId: {
				type: Number,
				default: 0
			},
		},
		emits: ['input', 'confirm'],
		watch: {
			value: {
				handler(nVal) {
					this.show = nVal
				},
				immediate: true
			},
			defaultId: {
				handler(nVal) {
					if (!nVal)
						return
					this.selectIndex = this.list.findIndex(item => item.id === this.defaultId)
				},
				immediate: true,
			}
		},
		data() {
			return {
				show: false, // 展示
				selectIndex: 0, // 选中索引
			}
		},
		methods: {
			/**
			 * @param {Boolean} flag
			 */
			handleShow(flag) {
				this.$emit('input', flag)
			},
			handleConfirm() {
				this.$emit('confirm', this.list[this.selectIndex])
				this.handleShow(false)
			},
			change(data, index) {
				this.selectIndex = index
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 0 30rpx 30rpx;
	}

	.title {
		margin: 30rpx auto 0;
		color: #000000;
		font-size: 28rpx;
		text-align: center;
	}

	.content {
		margin-top: 40rpx;
		max-height: 50vh;
	}

	.item {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
		padding: 25rpx 30rpx 40rpx 40rpx;
		border-radius: 20rpx;
		background: #ffeadf;
	}

	.item-left {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: $theme-color;

		.price {
			display: flex;
			align-items: flex-end;
			font-size: 40rpx;
			font-weight: 500;

			span {
				font-size: 60rpx;
				line-height: 100%;
			}
		}

		.term {
			font-size: 24rpx;
		}
	}

	.item-middle {
		margin-left: 50rpx;

		.name {
			color: #000000;
			font-size: 32rpx;
		}

		.desc {
			margin-top: 10rpx;
			color: #8c8c8c;
			font-size: 24rpx;
		}
	}

	.item-right {
		margin-left: auto;
	}

	.btn {
		margin: 20rpx auto 0;
		width: 690rpx;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 35rpx;
		background: $gradient-bg-color;
		color: #ffffff;
		text-align: center;
		font-size: 24rpx;
	}
</style>